import API from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    choseCategoryList: [],
    active: 0,
    smallActive: true,
    selectNum: 0,
    pcate_id: '',
    cate_id: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getchoseCategory()
  },
  // 获取分类
  getchoseCategory() {
    API.choseCategory({}).then(res => {
      console.log(res)
      if (res.code == 0) {
        var oneClass = res.data
        for (var k in oneClass) {
          if (wx.getStorageSync('pcate_id')) {
            if (oneClass[k].id == wx.getStorageSync('pcate_id')) {
              this.setData({
                active:k
              })
            }
            
          } else {
            wx.setStorageSync('pcate_id', oneClass[k].id)
            this.setData({
              pcate_id: oneClass[k].id
            })
          }
       
          if (oneClass[k].child.length > 0) {
            var child = oneClass[k].child
            for (var j in child) {
              if (wx.getStorageSync('cate_id')) {
                var getCateId = wx.getStorageSync('cate_id').split(',')
                for (var y in getCateId) {
                  if (child[j].id == getCateId[y]) {
                    child[j].smallActive = true
                  }
                }
                
                console.log()
              } else {
                child[j].smallActive = false
              }
             
            }
          }
        }
        console.log(oneClass)
        this.setData({
          choseCategoryList: oneClass
        })
      }
    })
  },
  selectClass(e) {
    var index = e.currentTarget.dataset.index
    var choseCategoryList = this.data.choseCategoryList
    var cate_id = this.data.cate_id
    for (var k in choseCategoryList) {
      if (choseCategoryList[k].child.length > 0) {
        var child = choseCategoryList[k].child
        console.log(index)
        child[index].smallActive = !child[index].smallActive
        for (var j in child) {
          if (child[j].smallActive) {
            cate_id.push({id:child[j].id,cate_name:child[j].cate_name})
          }
        }
        console.log(cate_id)
        this.setData({
          cate_id:cate_id
        })
    
      }
    }

    this.setData({
      choseCategoryList: choseCategoryList
    })

  },
  // 确定
  doneSelect() {

    var choseCategoryList = this.data.choseCategoryList
    var cate_id = []
    var cate_name = []

    for (var k in choseCategoryList) {
      if (choseCategoryList[k].child.length > 0) {
        var child = choseCategoryList[k].child
        console.log(child)
        console.log(8888)
        for (var j in child) {
          if (child[j].smallActive) {
            cate_id.push(child[j].id)
            cate_name.push(child[j].cate_name)
          }
        }

      }
    }
    console.log('wvod>>',this.data.pcate_id)
    if (cate_id.length>0 && cate_id.length<4) {
      wx.setStorageSync('cate_id', cate_id.join(','))
      wx.setStorageSync('pcate_id', this.data.pcate_id)
      wx.setStorageSync('pcate_name', cate_name.join(','))
      wx.navigateBack({
        delta: 1
      })
    } else {
      wx.showModal({
        title: '',
        content: '请选择分类并最多选三个',
      })
    }
  

  },
  // 导航切换
  navTab(e) {
    var index = e.currentTarget.dataset.index
    var pcate_id = e.currentTarget.dataset.id
    console.log(index)
    this.setData({
      active: index,
      pcate_id: pcate_id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})